package pres.wchen.bussiness.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import pres.wchen.bussiness.service.SysUserService;
import pres.wchen.bussiness.vo.SysUserVO;
import pres.wchen.bussiness.web.BaseController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @作者： wchen
 * @描述：
 * @创建时间： 2018/1/16 16:20
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */

@Controller
@RequestMapping("/sys")
public class LoginController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public String login(){
        return "/sys/login";
    }


    @PostMapping("/login")
    public ModelAndView doLogin(SysUserVO sysUser, HttpSession session){
        UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getUsername(), sysUser.getPassword());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            SysUserVO info = (SysUserVO) subject.getPrincipal();
            logger.error("--------------sysUser放到session---------" + info.toString());
            session.setAttribute("sysUser", info);
            logger.error("--------------登录成功---------" + info.toString());
            return super.dealInfoResult("/sys/user/info",info);
        }catch (Exception e){
            logger.error("--------------登录失败---------");
            e.printStackTrace();
            return new ModelAndView("/sys/login");
        }

//        SysUserVO sysUserVO = sysUserService.login(sysUser.getUsername(), sysUser.getPassword());
//        if(sysUserVO != null){
//            session.setAttribute("sysUser", sysUserVO);
//            logger.error("--------------登录成功---------");
//            return super.dealInfoResult("/sys/user/info",sysUserVO);
//        }else{
//            logger.error("--------------登录失败---------");
//            return new ModelAndView("/sys/login");
//        }
    }




    @GetMapping("/tokenTest")
    public void doLogin2(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Map map = new HashMap();
        System.out.println("**************************token验证中**********************");
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        System.out.println("signature:" + signature);
        System.out.println("timestamp:" + timestamp);
        System.out.println("nonce:" + nonce);
        System.out.println("echostr:" + echostr);
        map.put("echostr",echostr);

        PrintWriter pw = response.getWriter();
        pw.append(echostr);
        pw.flush();
        pw.close();
    }






}
